package htlgkr.nodlehs;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class RoomList extends ListActivity {

	SQLiteDatabase db;
	NodlehsDatabase bridge;
	Hashtable<String, String> roomT;
	ArrayList<String> ro;
	boolean isEmpty = false;
	ListView lv;
	ArrayAdapter<String> roomAdapter;
	Cursor rooms;
	String menheader = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.roomview);
		lv = (ListView) findViewById(android.R.id.list);
		ro = new ArrayList<String>();
		roomT = new Hashtable<String, String>();
		bridge = new NodlehsDatabase(this);
		db = bridge.getWritableDatabase();

		loadRooms();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.roommenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.men_newRoom:
			Intent i = new Intent(getApplicationContext(),
					CreateNewRoomActivity.class);
			i.putExtra("ROOMID", 0);
			startActivityForResult(i, 111);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.detail:
			Intent i = new Intent(getApplicationContext(),
					CreateNewRoomActivity.class);
			i.putExtra("ROOMID", getRoomID(menheader));
			startActivityForResult(i, 111);
			return true;
		case R.id.delete:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private int getRoomID(String string) {
		Cursor rooList = db.query("rooms", DBRoomsTbl.ALL_COLUMNS, "_id>?",
				new String[] { "0" }, null, null, null);
		int id = 0;
		while (rooList.moveToNext()) {
				if (rooList.getString(1).equals(string)) {
					id = Integer.parseInt(rooList.getString(0));
					
					
					break;
				}
			}
		Log.i("RoomID", id+"");
		return id;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (resultCode) {
		case 111:
			roomAdapter.clear();
			loadRooms();
		}

	}

	private void loadRooms() {

		rooms = db.query("rooms", DBRoomsTbl.ALL_COLUMNS, "_id>?",
				new String[] { "0" }, null, null, null);

		while (rooms.moveToNext()) {
			roomT.put(rooms.getString(0), rooms.getString(1));
			ro.add(rooms.getString(1));
		}
		if (ro.isEmpty()) {
			ro.add("Kein Objekt gefunden");
			isEmpty = true;
		}
		roomAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, ro);
		lv.setAdapter(roomAdapter);

		if (!isEmpty) {
			registerForContextMenu(lv);
			lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

				public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenuInfo menuInfo) {
					getMenuInflater().inflate(R.menu.contextmenue, menu);
					AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
					menu.setHeaderTitle(ro.get(info.position));
					menheader = ro.get(info.position);

				}
			});
		}

		//rooms.moveToFirst();

	}

	@Override
	protected void onDestroy() {
		db.close();
		super.onDestroy();
	}

}
